Component({
  properties: {
    buttons: {
      type: Array,
      value: []
    },
    mark: {
      type: Object,
      value: {}
    }
  },

  data: {
    slideX: 0,
    buttonsWidth: 160, // 按钮区域宽度
    startX: 0,
    isTransition: false
  },

  methods: {
    onTouchStart(e: any) {
      this.setData({
        startX: e.touches[0].clientX,
        isTransition: false
      });
    },

    onTouchMove(e: any) {
      const moveX = e.touches[0].clientX - this.data.startX;
      let slideX = moveX * 2; // 转换为rpx，增加灵敏度
      
      // 限制滑动范围
      if (slideX > 0) {
        slideX = 0;
      } else if (slideX < -this.data.buttonsWidth) {
        slideX = -this.data.buttonsWidth;
      }
      
      this.setData({ slideX });
    },

    onTouchEnd(e: any) {
      this.setData({ isTransition: true });
      
      // 根据滑动距离决定是否展开
      if (this.data.slideX < -this.data.buttonsWidth / 2) {
        this.setData({ slideX: -this.data.buttonsWidth });
      } else {
        this.setData({ slideX: 0 });
      }
    },

    onButtonTap(e: any) {
      const index = e.currentTarget.dataset.index;
      // 触发自定义事件，传递更多上下文信息
      const event = {
        index, 
        button: this.data.buttons[index],
        currentTarget: {
          dataset: this.dataset
        },
        mark: this.properties.mark || {}
      };
      this.triggerEvent('buttontap', event);
      // 重置位置
      this.setData({ 
        slideX: 0,
        isTransition: true 
      });
    },

    // 提供外部调用方法来重置位置
    reset() {
      this.setData({ 
        slideX: 0,
        isTransition: true 
      });
    }
  }
}); 